home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / jplay.zip / JPLAYIT.PAS < prev    next >
Pascal/Delphi Source File  |  1990-07-05  |  5KB  |  227 lines

  1. UNIT JPlayit;
  2.  
  3. (***************************************************************************
  4.  
  5. .TPU unit for playing and displaying .MUZ files
  6.           -Process .MUZ files using BINOBJ filename.MUZ filename.OBJ filename
  7.           -Change names in file JPLAY.PAS
  8.           -Julian Higgs 6-24-90
  9.           -
  10. ****************************************************************************)
  11.  
  12.  
  13.  
  14.  
  15. (*****************************************************)
  16. (* Copyright (c) 1988 by Neil J. Rubenking           *)
  17. (* Demonstrates how to play a PIANOMAN MUZ file from *)
  18. (* Turbo Pascal version 4.0.  You may freely include *)
  19. (* and distribute this Unit in your programs.        *)
  20. (*                                                   *)
  21. (* To use the Unit, first create a MUZ file using    *)
  22. (* PIANOMAN.  Then call on the BINOBJ utility that   *)
  23. (* comes with TP4 to turn the MUZ file into an OBJ   *)
  24. (* file.  Finally, declare a TP4 Procedure as an     *)
  25. (* EXTERNAL using that OBJ file.  Now you can call   *)
  26. (* the Procedure PlayOBJ in this Unit.               *)
  27. (*                                                   *)
  28. (* See PLAYDEMO.PAS for demonstration.               *)
  29. (*****************************************************)
  30.  
  31.  
  32.  
  33.  
  34. (**********************)
  35. (**)   INTERFACE    (**)
  36. (**********************)
  37. Uses CRT,GRAPH;
  38. PROCEDURE PlayOBJ(
  39.          P : Pointer; {Pointer to "fake External" procedure containing tune}
  40.    KeyStop : Boolean; {If true, tune will stop when key is pressed.}
  41.     VAR CH : char);   {^Returns pressed key if stopped.}
  42.  
  43.  
  44.  
  45.  
  46. (**********************)
  47. (**) IMPLEMENTATION (**)
  48. (**********************)
  49.  
  50. TYPE
  51.   FiledNote = RECORD
  52.                 O, NS : Byte;
  53.                 D : Word;
  54.               END;
  55.   NotePt = ^FiledNote;
  56.  
  57. VAR
  58.   Oct_Val : ARRAY[0..8] OF Real;
  59.   Freq_Val : ARRAY[1..12] OF Real;
  60.   Num, Notec, Ynote, Ynote2, Xnote2 : word;
  61.  
  62.  
  63.  
  64.   FUNCTION int2str (L : Longint) : String;
  65.   var S : String;
  66.   BEGIN
  67.     Str(L, S);
  68.     int2str :=S;
  69.   END;
  70.  
  71.  
  72.  
  73.   PROCEDURE Writeout (S : string);
  74.   BEGIN
  75.     outtextxy(505,35,S);
  76.   END;
  77.  
  78.  
  79.  
  80.  
  81.   PROCEDURE Set_Frequencies;
  82.   VAR N : Byte;
  83.   BEGIN
  84.     Freq_Val[1] := 1;
  85.     Freq_Val[2] := 1.0594630944;
  86.     FOR N := 3 TO 12 DO
  87.       Freq_Val[N] := Freq_Val[N - 1] * Freq_Val[2];
  88.     Oct_Val[0] := 32.70319566;
  89.     FOR N := 1 TO 8 DO
  90.       Oct_Val[N] := Oct_Val[N - 1] * 2;
  91.   END;
  92.  
  93.  
  94.  
  95.  
  96.  
  97.   PROCEDURE Pgrid;
  98.   BEGIN
  99.     setcolor(4);
  100.     line(1,2  ,639,2);
  101.     line(1,79 ,639,79 );
  102.     line(1,179,639,179);
  103.     line(1,279,639,279);
  104.     line(1,379,639,379);
  105.     line(1,479,639,479);
  106.     line(1,1  ,1  ,479);
  107.     line(200,1,200,479);
  108.     line(400,1,400,479);
  109.     line(600,1,600,479);
  110.     line(639,1,639,479);
  111.     setcolor(15);
  112.     outtextxy(5,1,  '480>');
  113.     outtextxy(5,76, '400>');
  114.     outtextxy(5,176,'300>');
  115.     outtextxy(5,276,'200>');
  116.     outtextxy(5,376,'100>');
  117.     outtextxy(5,470,'  0>');
  118.     writeout('='+int2str(num));
  119.   END;
  120.  
  121.  
  122.  
  123.  
  124.  
  125.   PROCEDURE Posnote;
  126.   BEGIN
  127.     Ynote2 := 480-(Round(Ynote/5));
  128.     if Ynote2 < 0   then Ynote2 :=0;
  129.     if Ynote2 > 479 then Ynote2 :=479;
  130.     Xnote2 := Xnote2 + 1;
  131.     if Xnote2 > 640 then
  132.       BEGIN
  133.         ClearDevice;
  134.         Pgrid;
  135.         setcolor(14);
  136.         Xnote2 := 1;
  137.       END;
  138.       outTextxy(xnote2,ynote2,'.');
  139.   END;
  140.  
  141.  
  142.  
  143.  
  144.  
  145.   PROCEDURE PlayOne(Octave, NoteStaccato : Byte; Duration : Integer);
  146.   CONST
  147.     factor : ARRAY[0..10] OF Real = (0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0);
  148.   VAR
  149.     Frequency : Real;
  150.     Note, Staccato : Byte;   (*!*)
  151.   BEGIN
  152.     Note := NoteStaccato SHR 4;
  153.     Staccato := NoteStaccato AND $F;
  154.     IF Staccato > 10 THEN Staccato := 10;
  155.     IF Staccato < 0 THEN Staccato := 0;
  156.     IF Octave > 8 THEN Octave := 8;
  157.     IF Octave < 1 THEN Octave := 1;
  158.     CASE Note OF
  159.       1..12 : BEGIN
  160.                 Frequency := Oct_Val[Octave] * Freq_Val[Note];
  161.                 Ynote := Round(Frequency);
  162.                 Posnote;
  163.                 Sound(Round(Frequency));
  164.                 Delay(Round(Duration * factor[10 - Staccato]));
  165.                 IF Duration > 0 THEN NoSound;
  166.                 Delay(Round(Duration * factor[Staccato]));
  167.               END;
  168.       13 : BEGIN NoSound; Delay(Duration); END;
  169.     END;                     {case}
  170.   END;
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. (****************************************************************************
  178.  
  179.                The callable object - rest is support routines              *)
  180.  
  181.  
  182.  
  183.  
  184.   PROCEDURE PlayOBJ(P : Pointer; KeyStop : Boolean; VAR CH : char);
  185.  
  186.   VAR T : NotePt;
  187.     N   : Word;
  188.  
  189.   BEGIN
  190.  
  191.     Xnote2 := 0;
  192.     ClearDevice;
  193.     SetColor(5);
  194.     OutTextxy(435,20,'Jewltronics 1990');
  195.     outtextxy(455,35,'note#=');
  196.     pgrid;
  197.     Setcolor(14);
  198.  
  199.     T := NotePt(P);
  200.     Inc(LongInt(T), SizeOf(FiledNote) * 5);
  201.     Num := LongInt(T^) AND $FFFF;
  202.     Inc(LongInt(T), SizeOf(FiledNote) * 4);
  203.     FOR N := 1 TO Num DO
  204.       BEGIN
  205.         WITH T^ DO
  206.           PlayOne(O, NS, D);
  207.         Inc(LongInt(T), SizeOf(FiledNote));
  208.         IF KeyStop AND KeyPressed THEN
  209.           BEGIN
  210.             CH := ReadKey;
  211.             Exit;
  212.           END;
  213.       END;
  214.   END;
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. (**********************)
  222. (*   INITIALIZATION   *)
  223. (**********************)
  224. BEGIN
  225.   Set_Frequencies;
  226. END.
  227.